define(function(require) {

    require('../../../c/base/index');

    require('../../../c/nav/index');

    var list = {
        init: function() {
            //初始化菜单选中效果
            $(".fa-circle-o").each(function() {
                if ($(this).parents("a").text() == "财务结算对账") {
                    $(this).parents("a").parents("li").addClass("active");
                }
            });
            this.initCity();
            this.initDatetimepicker();
            this.initValidator();
            this.bindEvent();
        },
        /**
         * 城市初始化
         */
        initCity: function() {
            CRM.request({
                "type": 'POST',
                "url": CRM.getOuterUrl('/shop/city/queryOpenCity'),
                "dataType": "json",
                contentType: "application/json",
                data: JSON.stringify({}),
                "success": function(req) {
                    if (req.status) {
                        var arr = [];
                        $.each(req.entry, function(index, item) {
                            arr.push("<option value='" + item.cityCode + "'>" + item.city + "</option>");
                        });
                        $("#city").append(arr.join(''));
                    } else {
                        CRM.showDialog(req.message);
                    }
                }
            });
        },
        /**
         * 表单验证初始化
         */
        initValidator: function() {
            $('#query_form').bootstrapValidator({
                message: '输入错误',
                feedbackIcons: {
                    valid: 'glyphicon glyphicon-ok',
                    invalid: 'glyphicon glyphicon-remove',
                    validating: 'glyphicon glyphicon-refresh'
                },
                fields: {
                    gmtCreateStart: {
                        message: '开始时间输入错误',
                        validators: {
                            notEmpty: {
                                message: '开始时间不能为空'
                            },
                            callback: {
                                message: '时间跨度不能超过7天',
                                callback: function(value, validator) {
                                    var gmtCreateStart = +new Date($("#gmtCreateStart").val()),
                                        gmtCreateEnd = +new Date($("#gmtCreateEnd").val());
                                    if (gmtCreateEnd - gmtCreateStart > 604800000) {
                                        return false;
                                    } else {
                                        return true;
                                    }
                                }
                            }
                        }
                    },
                    gmtCreateEnd: {
                        message: '结束时间输入错误',
                        validators: {
                            notEmpty: {
                                message: '结束时间不能为空'
                            },
                            callback: {
                                message: '时间跨度不能超过7天',
                                callback: function(value, validator) {
                                    var gmtCreateStart = +new Date($("#gmtCreateStart").val()),
                                        gmtCreateEnd = +new Date($("#gmtCreateEnd").val());
                                    if (gmtCreateEnd - gmtCreateStart > 604800000) {
                                        return false;
                                    } else {
                                        return true;
                                    }
                                }
                            }
                        }
                    }
                }
            });
            $('#query_bill_form').bootstrapValidator({
                message: '输入错误',
                feedbackIcons: {
                    valid: 'glyphicon glyphicon-ok',
                    invalid: 'glyphicon glyphicon-remove',
                    validating: 'glyphicon glyphicon-refresh'
                },
                fields: {
                    billDateStart: {
                        message: '开始时间输入错误',
                        validators: {
                            notEmpty: {
                                message: '开始时间不能为空'
                            },
                            callback: {
                                message: '时间跨度不能超过7天',
                                callback: function(value, validator) {
                                    var billDateStart = +new Date($("#billDateStart").val()),
                                        billDateEnd = +new Date($("#billDateEnd").val());
                                    if (billDateEnd - billDateStart > 604800000) {
                                        return false;
                                    } else {
                                        return true;
                                    }
                                }
                            }
                        }
                    },
                    billDateEnd: {
                        message: '结束时间输入错误',
                        validators: {
                            notEmpty: {
                                message: '结束时间不能为空'
                            },
                            callback: {
                                message: '时间跨度不能超过7天',
                                callback: function(value, validator) {
                                    var billDateStart = +new Date($("#billDateStart").val()),
                                        billDateEnd = +new Date($("#billDateEnd").val());
                                    if (billDateEnd - billDateStart > 604800000) {
                                        return false;
                                    } else {
                                        return true;
                                    }
                                }
                            }
                        }
                    }
                }
            });
            $('#query_shop_form').bootstrapValidator({
                message: '输入错误',
                feedbackIcons: {
                    valid: 'glyphicon glyphicon-ok',
                    invalid: 'glyphicon glyphicon-remove',
                    validating: 'glyphicon glyphicon-refresh'
                },
                fields: {
                    billDateStart: {
                        message: '开始时间输入错误',
                        validators: {
                            notEmpty: {
                                message: '开始时间不能为空'
                            },
                            callback: {
                                message: '时间跨度不能超过7天',
                                callback: function(value, validator) {
                                    var shopDateStart = +new Date($("#shopDateStart").val()),
                                        shopDateEnd = +new Date($("#shopDateEnd").val());
                                    if (shopDateEnd - shopDateStart > 604800000) {
                                        return false;
                                    } else {
                                        return true;
                                    }
                                }
                            }
                        }
                    },
                    billDateEnd: {
                        message: '结束时间输入错误',
                        validators: {
                            notEmpty: {
                                message: '结束时间不能为空'
                            },
                            callback: {
                                message: '时间跨度不能超过7天',
                                callback: function(value, validator) {
                                    var shopDateStart = +new Date($("#shopDateStart").val()),
                                        shopDateEnd = +new Date($("#shopDateEnd").val());
                                    if (shopDateEnd - shopDateStart > 604800000) {
                                        return false;
                                    } else {
                                        return true;
                                    }
                                }
                            }
                        }
                    }
                }
            });
        },
        /**
         * 初始化日期控件
         */
        initDatetimepicker: function() {
            $('.form_date').datetimepicker({
                language: 'zh-CN',
                weekStart: 1,
                todayBtn: 1,
                autoclose: 1,
                todayHighlight: 1,
                startView: 1,
                minView: 1,
                maxView: 2,
                forceParse: 0
            });
        },
        /**
         * 导出CVS
         */
        exportCVS: function() {
            var self = this;
            $("#query_form").data('bootstrapValidator').resetForm();
            $("#query_form").data('bootstrapValidator').validate();
            if ($("#query_form").data('bootstrapValidator').isValid()) {
                document.getElementById("query_form").submit();
            }
        },
        /**
         * 商户打款订单明细导出
         */
        exportBill: function() {
            var self = this;
            $("#query_bill_form").data('bootstrapValidator').resetForm();
            $("#query_bill_form").data('bootstrapValidator').validate();
            if ($("#query_bill_form").data('bootstrapValidator').isValid()) {
                document.getElementById("query_bill_form").submit();
            }
        },
        /**
         * 商户打款店铺维度数据导出
         */
        exportByShop: function() {
            var self = this;
            $("#query_shop_form").data('bootstrapValidator').resetForm();
            $("#query_shop_form").data('bootstrapValidator').validate();
            if ($("#query_shop_form").data('bootstrapValidator').isValid()) {
                document.getElementById("query_shop_form").submit();
            }
        },
        bindEvent: function() {
            var self = this;
            $("#query_form").attr('action', CRM.getOuterUrl('/trade/crm/order/downloadOrderForFinance'));
            $("#query_bill_form").attr('action', CRM.getOuterUrl('/centralbank/bill/downloadDisburseOrderForFinance'));
            $("#query_shop_form").attr('action', CRM.getOuterUrl('/centralbank/bill/downloadDisburseRecordOrganizedByShopForFinance'));
            $("#export_btn").on('click', function() {
               self.exportCVS();
               // alert('请联系杨秋华');
               return false;
            });
            $("#export_bill_btn").on('click', function() {
                self.exportBill();
            });
            $("#export_shop_btn").on('click', function() {
                self.exportByShop();
            });
            $('#timeType').change(function(event) {
                switch (this.value) {
                    case '0':
                        $('#gmtCreateStart').attr('name', 'gmtCreateStart');
                        $('#gmtCreateEnd').attr('name', 'gmtCreateEnd');
                        break;
                    case '1':
                        $('#gmtCreateStart').attr('name', 'payTimeStart');
                        $('#gmtCreateEnd').attr('name', 'payTimeEnd');
                        break;
                }
            });
        }
    };


    list.init();
});